package com.alinesno.cloud.portal.desktop.web.ucenter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.base.boot.entity.ManagerCodeEntity;
import com.alinesno.cloud.base.boot.entity.ManagerCodeTypeEntity;
import com.alinesno.cloud.base.boot.service.IManagerCodeService;
import com.alinesno.cloud.base.boot.service.IManagerCodeTypeService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.facade.wrapper.RestWrapper;
import com.alinesno.cloud.common.web.base.advice.TranslateCode;
import com.alinesno.cloud.common.web.base.controller.FeignMethodController;
import com.alinesno.cloud.common.web.base.form.FormToken;
import com.alinesno.cloud.common.web.login.aop.DataFilter;
import com.alinesno.cloud.common.web.login.session.CurrentAccountSession;

import cn.hutool.core.bean.BeanUtil;

/**
 * 字典控制层
 * @author WeiXiaoJin
 * @since 2018年11月27日 上午6:41:40
 */
@Controller
@RequestMapping("portal/ucenter/dictionary")
@Scope(SpringInstanceScope.PROTOTYPE)
public class DictionaryController extends FeignMethodController<ManagerCodeEntity , IManagerCodeService>  {

	private static final Logger log = LoggerFactory.getLogger(DictionaryController.class) ; 

	@Autowired
	private IManagerCodeService managerCodeFeigin ; 
	
	@Autowired
	private IManagerCodeTypeService managerCodeTypeFeigin ; 

//	/**
//	 * 保存新对象 
//	 * @param model
//	 * @param managerCodeDto
//	 * @return
//	 */
//	@FormToken(remove=true)
//	@ResponseBody
//	@PostMapping("/save")
//	public ResponseBean save(Model model , HttpServletRequest request, ManagerCodeEntity managerCodeDto) {
//		
//		managerCodeDto = managerCodeFeigin.save(managerCodeDto) ; 
//		
//		return ResponseGenerator.ok(null) ; 	
//	}
	
//	/**
//	 * 代码管理查询功能  
//	 * @return
//	 */
//	@FormToken(save=true)
//	@GetMapping("/add")
//    public void add(Model model , HttpServletRequest request){
//		List<ManagerCodeTypeEntity> codeTypes = managerCodeTypeFeigin.findAll() ; 
//		model.addAttribute("codeTypes", codeTypes) ; 
//    }
	
	/**
	 * 保存新对象 
	 * @param model
	 * @param managerCodeDto
	 * @return
	 */
	@FormToken(remove=true)
	@ResponseBody
	@PostMapping("/update")
	public ResponseBean update(Model model , HttpServletRequest request, ManagerCodeEntity managerApplicationDto) {
		
		ManagerCodeEntity dto = managerCodeFeigin.getOne(managerApplicationDto.getId()) ; 
		BeanUtil.copyProperties(managerApplicationDto, dto, copyOptions); 
		
		managerApplicationDto = managerCodeFeigin.save(dto) ; 
		return ResponseGenerator.ok(null) ; 	
	}
	
	
	@Override
	public void add(Model model, HttpServletRequest request) {
//		RestWrapper wrapper = RestWrapper.create() ; 
//		wrapper.eq("operatorId", CurrentAccountSession.get(request).getId()) ; 
//		List<ManagerCodeTypeEntity> codeTypes = managerCodeTypeFeigin.findAll(wrapper) ; 
//		model.addAttribute("codeTypes", codeTypes) ; 
		
		setCodeTypes(model);
	}
	
	private void setCodeTypes(Model model) {
		RestWrapper wrapper = RestWrapper.create() ; 
		wrapper.eq("operatorId", CurrentAccountSession.get(request).getId()) ; 
		List<ManagerCodeTypeEntity> codeTypes = managerCodeTypeFeigin.findAll(wrapper) ; 
		model.addAttribute("codeTypes", codeTypes) ; 
	}


	/**
	 * 代码管理查询功能  
	 * @return
	 */
	@FormToken(save=true)
	@Override
    public void modify(HttpServletRequest request ,Model model , String id){
		Assert.hasLength(id , "主键不能为空.");
		ManagerCodeEntity code = managerCodeFeigin.getOne(id) ; 
	
//		List<ManagerCodeTypeEntity> codeTypes = managerCodeTypeFeigin.findAll() ; 
//		model.addAttribute("codeTypes", codeTypes) ; 
		
		setCodeTypes(model);
		model.addAttribute("bean", code) ; 
    }
	
	/**
	 * 删除
	 */
	@ResponseBody
	@PostMapping("/delete")
    public ResponseBean delete(@RequestParam(value = "rowsId[]") String[] rowsId){
		log.debug("rowsId = {}" , ToStringBuilder.reflectionToString(rowsId));
		if(rowsId != null && rowsId.length > 0) {
			managerCodeFeigin.deleteByIds(rowsId); 
		}
		return ResponseGenerator.ok(null) ; 
    }

	@DataFilter
	@TranslateCode(value="[{hasStatus:has_status}]", plugin="dictionaryTypeTranslatePlugin")
	@ResponseBody
	@RequestMapping("/datatables")
    public DatatablesPageBean datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
		log.debug("page = {}" , ToStringBuilder.reflectionToString(page));
		return this.toPage(model, managerCodeFeigin , page) ;
    }

	@Override
	public IManagerCodeService getFeign() {
		return managerCodeFeigin;
	}

}
















